package adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sxpjyy.monkey.mobilenurse.R;

import java.util.List;

import objects.Section;

/**
 * Created by Monkey on 2017/3/7.
 * 科室列表适配器
 */

public class SectionListAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private Context mContext;
    private List<Section> list;
    private int which = -1;

    public SectionListAdapter(Context context) {
        mContext = context;
        this.inflater = LayoutInflater.from(context);
    }

    public void setWhich(int which) {
        this.which = which;
    }

    public void setList(List<Section> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        if (list != null && list.size() > 0) {
            return list.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (getCount() > 0 && list.get(position) != null) {
            return list.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_section, parent, false);
            holder.section_name = (TextView) convertView.findViewById(R.id.section_name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (list != null && list.size() > 0) {
            if (list.get(position) != null) {
                if (which == position) {
                    holder.section_name.setTextColor(mContext.getResources().getColor(R.color.colorRed));
                } else {
                    holder.section_name.setTextColor(mContext.getResources().getColor(R.color.colorBlue));
                }
                Section section = list.get(position);
                if (section.getKSMC() != null) {
                    holder.section_name.setText(section.getKSMC());
                }
            }
        }
        return convertView;
    }

    private class ViewHolder {
        private TextView section_name;
    }
}
